Grokking Microservices Design Patterns
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
The Course at a Glance
Strangler Fig Pattern
Introduction
The Problem: Legacy Systems
The Strangler Pattern: A Solution
The Architecture of the Strangler Pattern
Strangler Pattern: A Detailed Example
Key Insights and Implications
API Gateway Pattern
Introduction to the API Gateway Pattern
Advantages of API Gateway Pattern
API Gateway Pattern: An Example
Performance Implications
System Design Example
Backends for Frontends (BFF) Pattern
Introduction to BFF
The Problem: Traditional Backend Models
The Architecture of the BFF Pattern
BFF Pattern: An Example
Performance Implications
System Design Examples
Service Discovery Pattern
What is Service Discovery Pattern?
The Problem: Service Coordination in Distributed Systems
Service Discovery Pattern: A Solution
The Architecture of the Service Discovery Pattern
The Inner Workings of the Service Discovery Pattern
Service Discovery Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Security Considerations
Circuit Breaker Pattern
Introduction
The Problem: The Struggles of Distributed Systems and Service Failures
The Circuit Breaker Pattern: An Effective Shield Against Cascading Failures
Circuit Breaker Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Summary
Introduction
The Problem: Failure Propagation in Distributed Systems
The Bulkhead Pattern: A Solution
The Architecture
The Inner Workings
Bulkhead Pattern: A Example
Performance Implications and Special Considerations
System Design Examples
Conclusion
Introduction
The Retry Pattern: A Solution to Unreliable External Resources
The Architecture of the Retry Pattern
Retry Pattern: An Example
Performance Implications
Use Cases and System Design Examples
Conclusion
Introduction to the Sidecar Pattern
The Problem: Monolithic Application Management
A Solution to the Monolithic Mayhem
The Architecture of the Sidecar Pattern
Sidecar Pattern: Bringing Theory to Practice with an Example
Performance Implications
System Design Examples: Bringing the Sidecar Pattern to Life
Introduction to Saga Pattern
The Problem: Traditional Transaction Models
The Saga Pattern: A Solution
The Architecture of the Saga Pattern
The Inner Workings of the Saga Pattern
Saga Pattern: A Example
Performance Implications
System Design Examples
Conclusion
Introduction
The Problem: Managing Complex Interactions in Distributed Systems
Event-Driven Architecture: A Promising Solution
The Architecture of the Event-Driven Architecture Pattern
The Inner Workings of the Event-Driven Architecture Pattern
Event-Driven Architecture Pattern: An Example
Performance Implications and Special Considerations
Use Cases and System Design Examples
Conclusion
Introduction
The Problem: Traditional CRUD Operations
CQRS Pattern: A Solution
The Architecture of the CQRS Pattern
The Inner Workings of the CQRS Pattern
CQRS Pattern: An Example
Issues, Special Considerations, and Performance Implications
System Design Examples
Introduction
The Problem: Configuration Management in a Microservices Architecture
The Solution: Configuration Externalization Pattern
Unveiling the Architecture: How Does Configuration Externalization Work?
Delving into Code: An Example
Considerations and Implications
Use Cases and Real-world Examples
Conclusion
Embrace the Future of Software Architecture
Circuit Breaker Pattern: An Example

Let’s implement a basic Circuit Breaker in Java to illustrate the core algorithm. We won’t use any external libraries – just a simple class to demonstrate the state logic:

public class CircuitBreaker { private enum State { CLOSED, OPEN, HALF_OPEN } private State state = State.CLOSED; private int failureCount = 0; private final int failureThreshold; private final long openTimeout; // how long to stay open before trying half-open private long lastFailureTime = 0; public CircuitBreaker(int failureThreshold, long openTimeout) { this.failureThreshold = failureThreshold; this.openTimeout = openTimeout; } public synchronized <T> T call(Callable<T> remoteCall) throws Exception { // 1. If breaker is OPEN, check if timeout has passed to allow a half-open trial if (state == State.OPEN) { long timeSinceFailure = System.currentTimeMillis() - lastFailureTime; if (timeSinceFailure < openTimeout) { // Still within open timeout, reject the call fast throw new IllegalStateException("Circuit is OPEN. Failing fast without calling remote service."); } else { // Timeout passed, move to half-open and allow a trial call state = State.HALF_OPEN; } } // 2. Try to invoke the remote call (allowed if state is CLOSED or HALF_OPEN) try { T result = remoteCall.call(); // Call succeeded. Reset failure count and close the circuit (if it was half-open, now fully close it). failureCount = 0; state = State.CLOSED; return result; } catch (Exception e) { // Call failed. Increase the failure count. failureCount++; lastFailureTime = System.currentTimeMillis(); if (failureCount >= failureThreshold) { // Too many failures, trip the circuit to OPEN state. state = State.OPEN; } throw e; // Propagate the exception (or could return a fallback here) } } }

In the code above, CircuitBreaker tracks the number of failures and the current state. The call method wraps the execution of a remote operation (passed as a Callable<T>). Here’s how it works:

  1. Before calling the remote service, it checks the state. If the circuit is OPEN, the code checks if the open timeout has elapsed. If not (meaning it's too soon to retry), it fails fast by throwing an exception (IllegalStateException in this case) instead of calling the remote service. If the timeout has elapsed, the breaker moves to HALF_OPEN state, allowing one trial call to proceed.

  2. Executing the call: If the state is Closed or Half-Open, it attempts the remoteCall. This is where your actual remote service logic would run.

  3. After the call returns: If the call succeeds, we reset the failure counter and set the state to CLOSED. (In Half-Open, a success means the service is back, so we fully close the circuit again. In Closed, a success just keeps it closed and can reset any past failure count.) If the call throws an exception or times out, we increment the failure count and record the time of failure. If the failures have reached the failureThreshold, the circuit switches to OPEN (tripped). The exception is rethrown to the caller – in a real system, the caller might catch this and fall back to some default behavior.

Using this CircuitBreaker is straightforward. For example:

CircuitBreaker cb = new CircuitBreaker(3, 10000); // trip after 3 failures, 10s timeout try { String data = cb.call(() -> unreliableService.getData()); // use data... } catch (Exception e) { // Handle fallback logic because service call failed or circuit is open useCachedData(); }

In this example, if unreliableService.getData() fails 3 times in a row, the circuit breaker will open. Further attempts within 10 seconds will immediately throw an exception without calling getData() at all – the circuit breaker is preventing more failures. After 10 seconds, the next call will be allowed to try the service again (half-open). If that next call succeeds, the circuit closes and things go back to normal. If it fails, the circuit re-opens and will block calls for another 10 seconds.

This is a simplistic implementation to demonstrate the concept. In production, a circuit breaker might be more sophisticated – handling thread-safety, using a rolling window of failure metrics, or integrating with monitoring systems – but the core idea is the same.

The Circuit Breaker Pattern: An Effective Shield Against Cascading Failures
Performance Implications and Special Considerations
Mark as Completed

On this page

System Overview: Online Retail Store

Scenario for Circuit Breaker

Components for Coding Example:

The Pattern in Action: